<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Stellt alle Funktionen zum Auflisten und Editieren der Saisons zur VerfÃ¼gung
 *
 * @author Robin
 */

include "redirector.php";
include "../ff_includes/class_database.php";
include "../ff_includes/class_season.php";

$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ) {
  case 'list':
    listSeasons();
    break;
  case 'new':
    newSeason();
    break;
  case 'edit':
    editSeason();
    break;
  case 'save':
    saveSeason();
    break;
  case 'delete':
    deleteSeason();
    break;
  default:
    listSeasons();
}

function listSeasons() {
    $db = new Database();
    $seasonList = Season::getSeasonList();
    $numbSeasons = $seasonList['totalRows'];
    $seasons = $seasonList['seasonList'];
    
    require("templates/season_list.php");
}

function newSeason() {
    $db = new Database();
    $season = new Season();
    
    require("templates/season_edit.php");
}

function editSeason() {
    $db = new Database();
    $seasonId = (int)$_GET['seasonId'];
    $season = Season::getSeasonById($seasonId);
    
    require("templates/season_edit.php");
}

function saveSeason() {
    // speichern
    if ( isset( $_POST['saveChanges'] ) ) {
        $season = new Season($_POST);
        // update
        if ($season->getId() != 0) {
            $season->update();
        }
        // neuer Eintrag
        else {
            $season->insert();
        }
        header( "Location: season.php?action=list" );
    }
    // abbrechen
    elseif ( isset( $_POST['cancel'] ) ) {
        header( "Location: season.php?action=list" );
    }
}

function deleteSeason() {
    $season = Season::getSeasonById((int)$_GET['seasonId']);
    $season->delete();
    header( "Location: season.php?action=list" );
}
?>
